﻿// ****************************************************************
// Copyright 2012, Stephan Burguchev
// e-mail: me@sburg.net
// ****************************************************************
// *
using System;
using System.ComponentModel;
using System.Linq.Expressions;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media.Animation;
using Updater.Wpf.ViewModels;

namespace Updater.Wpf.Controls
{
    /// <summary>
    ///     Interaction logic for UpdaterPopupControl.xaml
    /// </summary>
    public partial class UpdaterPopupControl : UserControl
    {
        private const string HidingStoryboard = "HideControlStoryboard";
        private const string ShowingStoryboard = "ShowControlStoryboard";
        private readonly string propertyName;
        private double desiredHeight;

        public UpdaterPopupControl()
        {
            this.InitializeComponent();
            this.DataContextChanged += this.OnDataContextChanged;

            Expression<Func<bool>> s = () => ((UpdaterViewModel)this.DataContext).UserInteractionIsRequired;
            this.propertyName = ((MemberExpression)s.Body).Member.Name;
            var sb = (Storyboard)this.FindResource(ShowingStoryboard);
            sb.Completed += this.SbOnCompleted;
        }

        private void OnDataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            if (e.OldValue != null)
            {
                ((INotifyPropertyChanged)this.DataContext).PropertyChanged -= this.OnPropertyChanged;
            }

            if (e.NewValue != null)
            {
                ((INotifyPropertyChanged)this.DataContext).PropertyChanged += this.OnPropertyChanged;
            }
        }

        private void OnPropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            if (e.PropertyName != this.propertyName)
            {
                return;
            }

            Storyboard storyboard;
            if (((UpdaterViewModel)this.DataContext).UserInteractionIsRequired)
            {
                storyboard = (Storyboard)this.FindResource(ShowingStoryboard);
            }
            else
            {
                storyboard = (Storyboard)this.FindResource(HidingStoryboard);
            }

            storyboard.Begin(this);
        }

        private void SbOnCompleted(object sender, EventArgs eventArgs)
        {
            this.UpdateDesiredHeight();
        }

        private void UpdateDesiredHeight()
        {
            if (this.desiredHeight == 0.0d &&
                (this.desiredHeight = this.MeasureOverride(new Size(double.MaxValue, double.MaxValue)).Height) != 0.0d)
            {
                var sbShow = (Storyboard)this.FindResource(ShowingStoryboard);
                sbShow.Children[0].SetValue(DoubleAnimation.ToProperty, this.desiredHeight);
                var sbHide = (Storyboard)this.FindResource(HidingStoryboard);
                sbHide.Children[0].SetValue(DoubleAnimation.FromProperty, this.desiredHeight);
                this.Height = this.desiredHeight;
            }
        }
    }
}
